package com.hulk.ratel.web.control;


import com.hulk.ratel.common.exception.WebRteException;
import com.hulk.ratel.component.R;
import com.hulk.ratel.component.RS;
import com.hulk.ratel.service.InitiativeScanPayService;
import com.hulk.ratel.service.dto.InitiativeScanPayReqDTO;
import com.hulk.ratel.service.dto.InitiativeScanPayRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


/**
 * @Author: hulk
 * @E-mail: 29572320@qq.com
 * @Date: Create in 2018/8/31 下午10:14
 * @Version: 1.0.0
 * @Modify by:
 * Class description  主扫支付交易入口，报文解析和check
 */
@Api(basePath = "/api/v1", value="InitiativeScanPayController",description="订单支付类",tags={"订单支付类","URI:/v1/api"})
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping(value = "/api/v1")
public class InitiativeScanPayController extends AbsBaseCtl {


    private InitiativeScanPayService payService;

    //交易代码
    @ApiOperation(value = "excute", notes = "订单支付接口", tags = {"订单支付接口", "URI:/INITIATIVE_SCANPAY"}, httpMethod = "POST",
            response = String.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", paramType = "query", name = "partnerNo", value = "合作方编号", required = true),
            @ApiImplicitParam(dataType = "String", paramType = "query", name = "traceId", value = "合作方交易流水号", required = true),
            @ApiImplicitParam(dataType = "String", paramType = "query", name = "plainText", value = "交易报文", required = true),
            @ApiImplicitParam(dataType = "String", paramType = "query", name = "partnerAESKey", value = "AES秘钥", required = true),
    })
   /* @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 500, message = "处理错误")})*/
    @PostMapping(value = "/INITIATIVE_SCANPAY", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    @ResponseBody
    @Override
    public String excute(@RequestAttribute String partnerNo, @RequestAttribute String traceId,
                         @RequestAttribute String plainText, @RequestAttribute String partnerAESKey,
                         @RequestAttribute String intCutDate, @RequestAttribute String workId) {

        log.info("InitiativeScanPayController<主扫> 请求参数：workId:{}, partnerNo:{},traceId:{},plainText:{}.", new Object[]{workId, partnerNo, traceId, plainText});

        InitiativeScanPayRespDTO respDTO;
        //解析报文并check
        InitiativeScanPayReqDTO  reqDTO = this.preHandle(plainText, traceId, null, InitiativeScanPayReqDTO.class);
         //支付业务处理
        R<InitiativeScanPayRespDTO> resp = payService.excute(reqDTO,intCutDate,workId);
        if (RS.ok(resp)) {
            respDTO = resp.getData();
        } else {
            throw new WebRteException(workId,resp.getCode(),resp.getMsg());
        }


        //组装返回报文
        return this.postHandle(respDTO, partnerAESKey, partnerNo, workId);
    }

    /**
     * 初始化响应参数
     **/

    /*private InitiativeScanPayRespDTO initRespDTO(InitiativeScanPayReqDTO reqDto,
                                                 String workId,
                                                 String errCode,
                                                 String errMsg,
                                                 String qrCode) {
        InitiativeScanPayRespDTO respDto = (InitiativeScanPayRespDTO)initRespDTO(reqDto, workId, errCode, errMsg);
        respDto.setQrCode(qrCode);
        return respDto;
    }*/


}